import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

main() => runApp(MyApp());


class MyApp extends StatelessWidget {

  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {

    return MaterialApp(
      theme: ThemeData(primaryColor: Colors.green),
      home: WillHome());
  }
}

class WillHome extends StatefulWidget {
  const WillHome({Key? key}) : super(key: key);

  @override
  _WillHomeState createState() => _WillHomeState();
}

class _WillHomeState extends State<WillHome> {

  var time;

  @override
  Widget build(BuildContext context) {
    return WillPopScope(child: Home(), onWillPop: () async {

      if(time!=null && DateTime.now().difference(time).inSeconds<1){
        return true;
      }
      time=DateTime.now();
      ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text("快速点击2次退出APP",textAlign: TextAlign.center,)));
      // Scaffold.of(context).showBottomSheet((context){
      //   return Text("快速点击2次退出APP",textAlign: TextAlign.center,);
      // });
      return false;
    });;
  }
}



class Home extends StatefulWidget {
  const Home({Key? key}) : super(key: key);

  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  @override
  Widget build(BuildContext context) {
    //设置尺寸（填写设计中设备的屏幕尺寸）如果设计基于360dp * 690dp的屏幕
    ScreenUtil.init(
        BoxConstraints(
            maxWidth: MediaQuery.of(context).size.width,
            maxHeight: MediaQuery.of(context).size.height),
        designSize: Size(360, 720),
        orientation: Orientation.portrait);
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
      ),
      body: Center(
        child: Column(
          children: [
            ElevatedButton(
              onPressed: () {
                  showDialog(context: context, builder: (BuildContext context){
                    return AlertDialog(content: Text("是否关闭弹窗"),actions: [
                       TextButton(onPressed: (){}, child: Text("取消")),
                       TextButton(onPressed: (){
                         Navigator.of(context).pop(false);
                       }, child: Text("确定"))
                    ],);
                  });
              },
              child: Text("AlertDialog"),
              clipBehavior: Clip.antiAlias,
              style: ButtonStyle(
                  backgroundColor: MaterialStateProperty.all(Colors.green),
                  shape: MaterialStateProperty.all(RoundedRectangleBorder(
                      borderRadius: BorderRadius.all(Radius.circular(15))))),
            ),
            ElevatedButton(
              onPressed: () {
                showCupertinoDialog(context: context, builder: (BuildContext context){
                  return CupertinoAlertDialog(content: Text("是否关闭弹窗"),actions: [
                    CupertinoDialogAction(onPressed: (){

                    }, child: Text("取消")),
                    CupertinoDialogAction(onPressed: (){
                        Navigator.of(context).pop(false);
                    }, child: Text("确定"))
                  ],);
                });
              },
              child: Text(
                "ElevatedButton",
                textAlign: TextAlign.center,
              ),
              clipBehavior: Clip.antiAlias,
              style: ButtonStyle(
                  fixedSize: MaterialStateProperty.all(Size(100, 100)),
                  backgroundColor: MaterialStateProperty.all(Colors.green),
                  shape: MaterialStateProperty.all(CircleBorder())),
            ),
          ],
        ),
      ),
    );
  }
}
